import java.util.Arrays;

/**
 * 救生艇
 * LeetCode 881
 */

public class test4 {

    public int numRescueBoats(int[] people, int limit) {
        // 先按体重从小到大排序
        Arrays.sort(people);
        int count = 0;
        int left = 0,right = people.length-1;
        int sum = 0;
        while(left <= right){
            sum = left==right?people[left]:people[left]+people[right];
            if(sum>limit){
                right--;
            }else{
                left++;
                right--;
            }
            count++;
        }
        return count;
    }

}
